﻿using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
namespace LightSwitchApplication
{
    public partial class SearchFilteredVisits
    {
        partial void SearchFilteredVisits_InitializeDataWorkspace(List<IDataService> saveChangesTo)
        {
            if (this.Application.User.HasPermission(Permissions.CanEditVisit))//admin
            {
            }
            else if (this.Application.User.HasPermission(Permissions.CanSearchVisit) && this.Application.User.HasPermission(Permissions.CanManageMyVisit))
            {
                string userName = this.Application.User.FullName;
                //sales person
                foreach (SalesPerson item in this.DataWorkspace.ApplicationData.SalesPersons)
                {
                    if (item.aspnet_Profile != null && item.aspnet_Profile.PropertyValuesString == userName)
                    {
                        SelectedSalesPerson = item;
                        //this.FindControl("SelectedSalesPersion").IsReadOnly = true;
                        break;
                    }
                }

                //customer
                //foreach (Customer item in this.DataWorkspace.ApplicationData.Customers.Where(c => c.aspnet_Profile_UserId != null))
                //{
                //    if (item.aspnet_Profile != null && item.aspnet_Profile.PropertyValuesString == userName)
                //    {
                //      SelectedCustomer = item;
                //      this.FindControl("SelectedCustomer").IsReadOnly = true;
                //        break;
                //    }
                //}
            }
            else //who is that???
            {
                this.FindControl("Search").IsReadOnly = true;
            }

        }

        partial void Search_Execute()
        {
            FilteredVisits.Load();
        }

        partial void ClearFilter_Execute()
        {
            if (!this.FindControl("SelectedSalesPerson").IsReadOnly)
            {
                SelectedSalesPerson = null;
            }
            if (!this.FindControl("SelectedShop").IsReadOnly)
            {
                SelectedShop = null;
            }
            if (!this.FindControl("SelectedVisitType").IsReadOnly)
            {
                SelectedVisitType = null;
            }
            if (!this.FindControl("SelectedVisitGoal").IsReadOnly)
            {
                SelectedVisitGoal = null;
            }
        }

        partial void Cancel_Execute()
        {
            this.Close(false);
        }

        partial void SearchFilteredVisits_Activated()
        {
            try
            {
                this.FindControl("SelectedSalesPersion").IsEnabled = (this.SelectedSalesPerson == null);
                //this.FindControl("SelectedCustomer").IsEnabled = (this.SelectedCustomer == null);
            }
            catch (Exception)
            {
            }
        }
    }
}
